package com.lh.runmanage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh.runmanage.model.InPutParam.UserTimeInParam;
import com.lh.runmanage.model.InPutParam.UserTimeListInParam;
import com.lh.runmanage.model.ResultVOPage;
import com.lh.runmanage.model.UserTimeModel;
import com.lh.runmanage.service.ProjectAndProductService;
import com.lh.runmanage.service.UserTimeService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.LhClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-22 17:30
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/userTimeController")
@Api(value = "仅展示用户使用时长", description = "系统运行态势平台展示湍")
public class UserTimeController {
    @Autowired
    UserTimeService userTimeService;
    @Autowired
    GetPropertiesClass getPropertiesClass;
    @Autowired
    ProjectAndProductService projectAndProductService;

    /**
     * 展示端方法：U1， 用户时长，方法ID：SE20210722172621453B158E11F707C
     *
     * @param projectId  表vou_usetimeLength,字段名projectId:项目ID
     * @param createTime 表vou_usetimeLength,字段名createTime:数据生成时间
     * @param page       当前页数
     * @param limit      每页条数
     * @return 分页数据
     */
    @ApiOperation(value = "展示端方法：U1， 用户时长[分页数据],方法ID：SE20210722172621453B158E11F707C", notes = "/userTimeController/V1/selectUserTimeByPageAndCreate?projectId=projectId&createTime=createTime&page=page&limit=limit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "数据生成时间", dataType = "Date")
            , @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String")
            , @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/V1/selectUserTimeByPageAndCreate")
    public ResultVOPage selectUserTimeByPageAndCreate(@RequestParam(value = "projectId") String projectId
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) throws UnsupportedEncodingException {
        if (projectId != null) {
            projectId = projectId.trim();
            if (projectId.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "projectId"), ResultVOPage.class, null);
            }
        }

        if (createTime == null) {
            createTime = LhClass.getDefaultDate();
        }
        UserTimeInParam userTimeInParam = new UserTimeInParam();
        userTimeInParam.setProjectId(projectId);
        userTimeInParam.setCreateTime(createTime);

        PageHelper.startPage(page, limit);
        List<UserTimeModel> userTimeModels = userTimeService.selectUserTimeByPageAndCreate(userTimeInParam);
        PageInfo pageInfo = new PageInfo<>(userTimeModels, limit);
        return ResultStruct.successPage(userTimeModels, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }

    /**
     * 展示端方法：U1， 用户时长，方法ID：SE20210722172621453B158E11F707C
     *
     * @param productId  表vou_usetimeLength,字段名projectId:项目ID
     * @param createTime 表vou_usetimeLength,字段名createTime:数据生成时间
     * @param page       当前页数
     * @param limit      每页条数
     * @return 分页数据
     */
    @ApiOperation(value = "展示端方法：P6， 用户时长[分页数据],方法ID：SE20210722172621453B158E11F707D", notes = "/userTimeController/V1/selectUserTimeByPageAndCreate?projectId=projectId&createTime=createTime&page=page&limit=limit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "数据生成时间", dataType = "Date")
            , @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String")
            , @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/V1/selectP6")
    public ResultVOPage selectUserTimeByPageAndCreateAndProjectIdList(@RequestParam(value = "productId") String productId
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {

        final List<String> list = projectAndProductService.selectProjectListByProductId(productId);
        if (createTime == null) {
            createTime = LhClass.getDefaultDate();
        }

        if (list.isEmpty()) {
            return ResultStruct.error("该产品，未包括项目", ResultVOPage.class, null);
        }
        UserTimeListInParam userTimeListInParam = new UserTimeListInParam();
        userTimeListInParam.setProjectId(list);
        userTimeListInParam.setCreateTime(createTime);
        userTimeListInParam.setProductCode(productId);

        PageHelper.startPage(page, limit);
        List<UserTimeModel> userTimeModels = userTimeService.selectUserTimeByPageAndCreateProjectList(userTimeListInParam);
        PageInfo pageInfo = new PageInfo<>(userTimeModels, limit);
        return ResultStruct.successPage(userTimeModels, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }
}
